package com.jackchong.widget

import android.content.Context
import android.widget.Button
import com.jackchong.widget.JTextView
import android.util.AttributeSet
import android.content.res.TypedArray
import com.utils.R
import android.text.TextUtils
import com.jackchong.base.BaseApplication
import android.graphics.Paint
import android.view.View

/**
 * 作者: jack(黄冲)
 * 邮箱: 907755845@qq.com
 * create on 2018/11/20 14:28
 */
class JButton @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : androidx.appcompat.widget.AppCompatButton(context, attrs, defStyleAttr) {

    var onSelectedChangeListener: ((v: View, selected: Boolean) -> Unit)? = null

    init {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.JButton)
        parseAttr(typedArray)
        typedArray.recycle()
    }

    private fun parseAttr(typedArray: TypedArray) {
        val pLine = typedArray.getBoolean(R.styleable.JButton_JText_P, false)
        val text = typedArray.getString(R.styleable.JButton_JText)
        val select = typedArray.getBoolean(R.styleable.JButton_JSelect, false)
        if (select) {
            isSelected = select
        }
        if (!TextUtils.isEmpty(text) && !BaseApplication.RELEASE_MODE) {
            setText(text)
        }
        if (pLine) {
            paint.flags = Paint.UNDERLINE_TEXT_FLAG
        }
    }

    val string: String
        get() = text.toString()


    override fun setSelected(selected: Boolean) {
        super.setSelected(selected)
        onSelectedChangeListener?.invoke(this, selected)
    }
}